package com.google.android.exoplayer2.offline;

import android.app.Notification;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import com.google.android.exoplayer2.offline.DownloadManager.TaskState;
import com.google.android.exoplayer2.scheduler.Requirements;
import com.google.android.exoplayer2.util.NotificationUtil;
import java.util.HashMap;
import o.ahp;
import o.ahw;
import o.ahz;
import o.amz;
import o.anr;

public abstract class DownloadService extends Service {
    public static final String ACTION_ADD = "com.google.android.exoplayer.downloadService.action.ADD";
    public static final String ACTION_INIT = "com.google.android.exoplayer.downloadService.action.INIT";
    public static final String ACTION_RELOAD_REQUIREMENTS = "com.google.android.exoplayer.downloadService.action.RELOAD_REQUIREMENTS";
    public static final long DEFAULT_FOREGROUND_NOTIFICATION_UPDATE_INTERVAL = 1000;
    public static final int FOREGROUND_NOTIFICATION_ID_NONE = 0;
    public static final String KEY_DOWNLOAD_ACTION = "download_action";
    public static final String KEY_FOREGROUND = "foreground";
    private static final HashMap<Class<? extends DownloadService>, b> ˋ = new HashMap();
    private static final Requirements ˏ = new Requirements(1, false, false);
    private int ʻ;
    private boolean ʼ;
    private DownloadManager ʽ;
    @Nullable
    private final a ˊ;
    @StringRes
    private final int ˎ;
    @Nullable
    private final String ॱ;
    private e ॱॱ;
    private boolean ᐝ;

    final class a implements Runnable {
        private final Handler ˊ = new Handler(Looper.getMainLooper());
        private final long ˋ;
        private boolean ˎ;
        private final int ˏ;
        final /* synthetic */ DownloadService ॱ;
        private boolean ॱॱ;

        public a(DownloadService downloadService, int i, long j) {
            this.ॱ = downloadService;
            this.ˏ = i;
            this.ˋ = j;
        }

        public void ˊ() {
            this.ˎ = true;
            ˎ();
        }

        public void ॱ() {
            this.ˎ = false;
            this.ˊ.removeCallbacks(this);
        }

        public void ˎ() {
            this.ॱ.startForeground(this.ˏ, this.ॱ.getForegroundNotification(this.ॱ.ʽ.ˎ()));
            this.ॱॱ = true;
            if (this.ˎ) {
                this.ˊ.removeCallbacks(this);
                this.ˊ.postDelayed(this, this.ˋ);
            }
        }

        public void ˏ() {
            if (!this.ॱॱ) {
                ˎ();
            }
        }

        public void run() {
            ˎ();
        }
    }

    static final class b implements o.ahw.e {
        @Nullable
        private final ahz ˊ;
        private final ahw ˋ;
        private final Requirements ˎ;
        private final Class<? extends DownloadService> ˏ;
        private final Context ॱ;

        private b(Context context, Requirements requirements, @Nullable ahz o_ahz, Class<? extends DownloadService> cls) {
            this.ॱ = context;
            this.ˎ = requirements;
            this.ˊ = o_ahz;
            this.ˏ = cls;
            this.ˋ = new ahw(context, this, requirements);
        }

        public void ˎ() {
            this.ˋ.ˋ();
        }

        public void ˋ() {
            this.ˋ.ˊ();
            if (this.ˊ != null) {
                this.ˊ.ˊ();
            }
        }

        public void ˏ(ahw o_ahw) {
            try {
                ॱ();
                if (this.ˊ != null) {
                    this.ˊ.ˊ();
                }
            } catch (Exception e) {
            }
        }

        public void ˊ(ahw o_ahw) {
            try {
                ॱ();
            } catch (Exception e) {
            }
            if (this.ˊ != null) {
                if (!this.ˊ.ˏ(this.ˎ, this.ॱ.getPackageName(), "com.google.android.exoplayer.downloadService.action.RESTART")) {
                    amz.ˋ("DownloadService", "Scheduling downloads failed.");
                }
            }
        }

        private void ॱ() throws Exception {
            try {
                this.ॱ.startService(DownloadService.ˋ(this.ॱ, this.ˏ, DownloadService.ACTION_INIT));
            } catch (Throwable e) {
                throw new Exception(e);
            }
        }
    }

    final class e implements com.google.android.exoplayer2.offline.DownloadManager.b {
        final /* synthetic */ DownloadService ˎ;

        private e(DownloadService downloadService) {
            this.ˎ = downloadService;
        }

        public void ˋ(DownloadManager downloadManager, TaskState taskState) {
            this.ˎ.onTaskStateChanged(taskState);
            if (this.ˎ.ˊ == null) {
                return;
            }
            if (taskState.ˊ == 1) {
                this.ˎ.ˊ.ˊ();
            } else {
                this.ˎ.ˊ.ˎ();
            }
        }

        public final void ˊ(DownloadManager downloadManager) {
            this.ˎ.ˋ();
        }
    }

    protected abstract DownloadManager getDownloadManager();

    @Nullable
    protected abstract ahz getScheduler();

    protected DownloadService(int i) {
        this(i, 1000);
    }

    protected DownloadService(int i, long j) {
        this(i, j, null, 0);
    }

    protected DownloadService(int i, long j, @Nullable String str, @StringRes int i2) {
        this.ˊ = i == 0 ? null : new a(this, i, j);
        this.ॱ = str;
        this.ˎ = i2;
    }

    public static Intent buildAddActionIntent(Context context, Class<? extends DownloadService> cls, ahp o_ahp, boolean z) {
        return ˋ(context, cls, ACTION_ADD).putExtra(KEY_DOWNLOAD_ACTION, o_ahp.ˊ()).putExtra(KEY_FOREGROUND, z);
    }

    public static void startWithAction(Context context, Class<? extends DownloadService> cls, ahp o_ahp, boolean z) {
        Intent buildAddActionIntent = buildAddActionIntent(context, cls, o_ahp, z);
        if (z) {
            anr.ˊ(context, buildAddActionIntent);
        } else {
            context.startService(buildAddActionIntent);
        }
    }

    public static void start(Context context, Class<? extends DownloadService> cls) {
        context.startService(ˋ(context, cls, ACTION_INIT));
    }

    public static void startForeground(Context context, Class<? extends DownloadService> cls) {
        anr.ˊ(context, ˋ(context, cls, ACTION_INIT).putExtra(KEY_FOREGROUND, true));
    }

    public void onCreate() {
        ˎ("onCreate");
        if (this.ॱ != null) {
            NotificationUtil.ˎ(this, this.ॱ, this.ˎ, 2);
        }
        this.ʽ = getDownloadManager();
        this.ॱॱ = new e();
        this.ʽ.ˊ(this.ॱॱ);
    }

    /* JADX WARNING: inconsistent code. */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public int onStartCommand(android.content.Intent r6, int r7, int r8) {
        /*
        r5 = this;
        r2 = 1;
        r1 = 0;
        r5.ʻ = r8;
        r5.ᐝ = r1;
        r0 = 0;
        if (r6 == 0) goto L_0x0024;
    L_0x0009:
        r3 = r6.getAction();
        r4 = r5.ʼ;
        r0 = "foreground";
        r0 = r6.getBooleanExtra(r0, r1);
        if (r0 != 0) goto L_0x001f;
    L_0x0017:
        r0 = "com.google.android.exoplayer.downloadService.action.RESTART";
        r0 = r0.equals(r3);
        if (r0 == 0) goto L_0x008a;
    L_0x001f:
        r0 = r2;
    L_0x0020:
        r0 = r0 | r4;
        r5.ʼ = r0;
        r0 = r3;
    L_0x0024:
        if (r0 != 0) goto L_0x0028;
    L_0x0026:
        r0 = "com.google.android.exoplayer.downloadService.action.INIT";
    L_0x0028:
        r3 = new java.lang.StringBuilder;
        r3.<init>();
        r4 = "onStartCommand action: ";
        r3 = r3.append(r4);
        r3 = r3.append(r0);
        r4 = " startId: ";
        r3 = r3.append(r4);
        r3 = r3.append(r8);
        r3 = r3.toString();
        r5.ˎ(r3);
        r3 = -1;
        r4 = r0.hashCode();
        switch(r4) {
            case -871181424: goto L_0x0095;
            case -608867945: goto L_0x00a9;
            case -382886238: goto L_0x009f;
            case 1015676687: goto L_0x008c;
            default: goto L_0x0050;
        };
    L_0x0050:
        r1 = r3;
    L_0x0051:
        switch(r1) {
            case 0: goto L_0x006c;
            case 1: goto L_0x006c;
            case 2: goto L_0x00b3;
            case 3: goto L_0x00d2;
            default: goto L_0x0054;
        };
    L_0x0054:
        r1 = "DownloadService";
        r3 = new java.lang.StringBuilder;
        r3.<init>();
        r4 = "Ignoring unrecognized action: ";
        r3 = r3.append(r4);
        r0 = r3.append(r0);
        r0 = r0.toString();
        o.amz.ˋ(r1, r0);
    L_0x006c:
        r0 = r5.getRequirements();
        r1 = r0.ॱ(r5);
        if (r1 == 0) goto L_0x00d6;
    L_0x0076:
        r1 = r5.ʽ;
        r1.ˋ();
    L_0x007b:
        r5.ˏ(r0);
        r0 = r5.ʽ;
        r0 = r0.ॱ();
        if (r0 == 0) goto L_0x0089;
    L_0x0086:
        r5.ˋ();
    L_0x0089:
        return r2;
    L_0x008a:
        r0 = r1;
        goto L_0x0020;
    L_0x008c:
        r4 = "com.google.android.exoplayer.downloadService.action.INIT";
        r4 = r0.equals(r4);
        if (r4 == 0) goto L_0x0050;
    L_0x0094:
        goto L_0x0051;
    L_0x0095:
        r1 = "com.google.android.exoplayer.downloadService.action.RESTART";
        r1 = r0.equals(r1);
        if (r1 == 0) goto L_0x0050;
    L_0x009d:
        r1 = r2;
        goto L_0x0051;
    L_0x009f:
        r1 = "com.google.android.exoplayer.downloadService.action.ADD";
        r1 = r0.equals(r1);
        if (r1 == 0) goto L_0x0050;
    L_0x00a7:
        r1 = 2;
        goto L_0x0051;
    L_0x00a9:
        r1 = "com.google.android.exoplayer.downloadService.action.RELOAD_REQUIREMENTS";
        r1 = r0.equals(r1);
        if (r1 == 0) goto L_0x0050;
    L_0x00b1:
        r1 = 3;
        goto L_0x0051;
    L_0x00b3:
        r0 = "download_action";
        r0 = r6.getByteArrayExtra(r0);
        if (r0 != 0) goto L_0x00c3;
    L_0x00bb:
        r0 = "DownloadService";
        r1 = "Ignoring ADD action with no action data";
        o.amz.ˋ(r0, r1);
        goto L_0x006c;
    L_0x00c3:
        r1 = r5.ʽ;	 Catch:{ IOException -> 0x00c9 }
        r1.ˋ(r0);	 Catch:{ IOException -> 0x00c9 }
        goto L_0x006c;
    L_0x00c9:
        r0 = move-exception;
        r1 = "DownloadService";
        r3 = "Failed to handle ADD action";
        o.amz.ˊ(r1, r3, r0);
        goto L_0x006c;
    L_0x00d2:
        r5.ˎ();
        goto L_0x006c;
    L_0x00d6:
        r1 = r5.ʽ;
        r1.ˊ();
        goto L_0x007b;
        */
        throw new UnsupportedOperationException("Method not decompiled: com.google.android.exoplayer2.offline.DownloadService.onStartCommand(android.content.Intent, int, int):int");
    }

    public void onTaskRemoved(Intent intent) {
        ˎ("onTaskRemoved rootIntent: " + intent);
        this.ᐝ = true;
    }

    public void onDestroy() {
        ˎ("onDestroy");
        if (this.ˊ != null) {
            this.ˊ.ॱ();
        }
        this.ʽ.ˋ(this.ॱॱ);
        ˏ();
    }

    @Nullable
    public IBinder onBind(Intent intent) {
        return null;
    }

    protected Requirements getRequirements() {
        return ˏ;
    }

    protected Notification getForegroundNotification(TaskState[] taskStateArr) {
        throw new IllegalStateException(getClass().getName() + " is started in the foreground but getForegroundNotification() is not implemented.");
    }

    protected void onTaskStateChanged(TaskState taskState) {
    }

    private void ˏ(Requirements requirements) {
        if (this.ʽ.ˏ() != 0) {
            Class cls = getClass();
            if (((b) ˋ.get(cls)) == null) {
                b bVar = new b(this, requirements, getScheduler(), cls);
                ˋ.put(cls, bVar);
                bVar.ˎ();
                ˎ("started watching requirements");
            }
        }
    }

    private void ˏ() {
        if (this.ʽ.ˏ() <= 0) {
            ˎ();
        }
    }

    private void ˎ() {
        b bVar = (b) ˋ.remove(getClass());
        if (bVar != null) {
            bVar.ˋ();
            ˎ("stopped watching requirements");
        }
    }

    private void ˋ() {
        if (this.ˊ != null) {
            this.ˊ.ॱ();
            if (this.ʼ && anr.ˋ >= 26) {
                this.ˊ.ˏ();
            }
        }
        if (anr.ˋ >= 28 || !this.ᐝ) {
            ˎ("stopSelf(" + this.ʻ + ") result: " + stopSelfResult(this.ʻ));
            return;
        }
        stopSelf();
        ˎ("stopSelf()");
    }

    private void ˎ(String str) {
    }

    private static Intent ˋ(Context context, Class<? extends DownloadService> cls, String str) {
        return new Intent(context, cls).setAction(str);
    }
}
